package cc.rengu.oltp.service.common.dao.impl;

import cc.rengu.jradp.mods.xml.XmlTree;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.common.dao.CertInfoMapper;
import cc.rengu.oltp.service.common.entity.CertInfo;
import cc.rengu.oltp.utility.util.DateUtil;
import cc.rengu.oltp.utility.util.DbsUtil;
import cc.rengu.oltp.utility.util.XmlConfigUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import cc.rengu.utility.cache.UnifiedCache;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;

/**
 * 证书信息表操作接口实现类
 * Created by 王成 on 2020/2/10
 */
public class CertInfoMapperImpl implements CertInfoMapper {
    private final RgLogger rglog = RgLog.getLogger(this.getClass().getName());
    private String dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(AppParamConstant.DATABASE_CONF_NODE, AppParamConstant.OLTP_POOL_NAME);

    public CertInfoMapperImpl() throws Exception {
        if (XmlTree.hasThreadTree()) {
            XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
            String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
            if (null != corporation && !corporation.isEmpty()) {
                String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
                dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppParamConstant.OLTP_POOL_NAME);
            }
        }
    }

    @Override
    public int insertCertInfo(CertInfo certInfo) throws Exception {
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "insertCertInfo");
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, certInfo, null);
        if (0 != iReturnCode) {
            rglog.error("登记证书信息失败,certIndex:<{}>,iReturnCode:<{}>", certInfo.getCertIndex(), iReturnCode);
        }
        return iReturnCode;
    }

    @Override
    public int updateCertInfoByPrimaryKey(CertInfo certInfo) throws Exception {
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "updateCertInfoByPrimaryKey");
        certInfo.setUpdateTime(DateUtil.getCurrentDateTime("yyyy-MM-dd HH:mm:ss.SSS"));
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, certInfo, null);
        if (0 != iReturnCode) {
            rglog.error("根据主键更新证书信息失败,certIndex:<{}>,iReturnCode:<{}>", certInfo.getCertIndex(), iReturnCode);
        }
        return iReturnCode;
    }

    @Override
    public CertInfo selectCertInfoByPrimaryKey(String certIndex) throws Exception {
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectCertInfoByPrimaryKey");
        CertInfo certInfo = new CertInfo();
        certInfo.setCertIndex(certIndex);
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, certInfo, certInfo);
        if (0 != iReturnCode) {
            rglog.warn("根据主键查询证书信息失败,certIndex:<{}>,iReturnCode:<{}>", certIndex, iReturnCode);
            return null;
        }
        return certInfo;
    }
}
